package com.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.comm.DataAndView;
import com.erp.comm.ResultCode;
import com.erp.pojo.Customer;
import com.erp.service.CustomerService;
import com.erp.vo.CustomerVo;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;


    @RequestMapping("loadAllCustomer")
    public DataAndView loadAllCustomer(CustomerVo customerVo) {

        IPage<Customer>page = new Page<>(customerVo.getPage(),customerVo.getLimit());
        QueryWrapper<Customer> qw = new QueryWrapper<>();
        if (customerVo.getCustomername() !=null) {
            qw.like("customername", customerVo.getCustomername());
        }
        if (customerVo.getPhone() !=null) {
            qw.like("phone", customerVo.getPhone());
        }
        if (customerVo.getConnectionperson() !=null) {
            qw.like("connectionperson", customerVo.getConnectionperson());
        }
        customerService.page(page,qw);
        DataAndView da = new DataAndView(page.getTotal(),page.getRecords());

        return da;


    }


    //添加
    @RequestMapping("addCustomer")
    public ResultCode addCustomer(Customer customer) {
        //获取用户填写的数据
        try{
            //清空id
            customer.setId(null);
            boolean bool = customerService.save(customer);
            if(bool){
                return ResultCode.SUCCESS_ADD;
            }else{
                return ResultCode.FAIL_ADD;
            }
        }catch (Exception e){
            return ResultCode.FAIL_ADD;
        }



    }


    //修改
    @RequestMapping("updateCustomer")
    public ResultCode updateCustomer(Customer customer) {
        try{
            boolean bool = customerService.updateById(customer);
            if(bool){
                return ResultCode.SUCCESS_UPDATE;
            }else{
                return ResultCode.FAIL_UPDATE;
            }
        }catch (Exception e){
            return ResultCode.FAIL_UPDATE;
        }

    }


    //删除
    @RequestMapping("deleteCustomer")
    public ResultCode deleteCustomer(CustomerVo customerVo) {
        try{
            boolean bool = customerService.removeById(customerVo);
            if(bool){
                return ResultCode.SUCCESS_DELETE;
            }else{
                return ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){
            return ResultCode.FAIL_DELETE;
        }


    }
    //批量删除
    @RequestMapping("batchDeleteCustomer")
    public ResultCode batchDeleteCustomer(CustomerVo customerVo) {

        List<Integer> list = new ArrayList<>();
        Integer[] ids = customerVo.getIds();
        for (Integer id : ids) {
            list.add(id);
        }

        //获取用户填写的数据
        try{
            boolean bool = customerService.removeByIds(list);
            if(bool){
                return ResultCode.SUCCESS_DELETE;
            }else{
                return ResultCode.FAIL_DELETE;
            }
        }catch (Exception e){
            return ResultCode.FAIL_DELETE;
        }

    }

}
